package com.zushiye.order.service;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.io.InputStream;

@Service
public class JsonFileReadService {

    @Autowired
    private ResourceLoader resourceLoader;

    public <T> T readJsonFile(String filePath, Class<T> clazz) throws IOException {
        Resource resource = resourceLoader.getResource("classpath:" + filePath);
        try (InputStream inputStream = resource.getInputStream()) {
            ObjectMapper objectMapper = new ObjectMapper();
            return objectMapper.readValue(inputStream, clazz);
        } catch (JsonProcessingException e) {
            throw new RuntimeException("Failed to parse JSON file", e);
        }
    }
}